package com.koron.tool.controller;

import com.github.pagehelper.PageInfo;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.koron.bean.base.Response;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.annocation.CasUser;
import com.koron.tool.bean.*;
import com.koron.tool.bean.dto.InventoryReportDetailDto;
import com.koron.tool.bean.dto.InventoryReportDto;
import com.koron.tool.convertor.InventoryReportConvertor;
import com.koron.tool.convertor.InventoryReportDetailConvertor;
import com.koron.tool.service.ToolInventoryService;
import com.koron.tool.util.PdfUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpStatus;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;


/**
 * @Author: laiguangming
 * @Date: 2022/10/8 10:45
 */
@Slf4j
@Api(tags = "工器具盘点")
@RestController
@RequestMapping("/inventory")
public class ToolInventoryController {

    @Autowired
    private ToolInventoryService service;

    @ApiOperation(value = "库房盘点")
    @GetMapping("/getInventoryStatus")
    public Response<PageInfo> getInventoryStatus(@CasUser @ApiIgnore EamUser user, ToolInventoryQuery query) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getInventoryStatus", Response.class, query);
    }

    @ApiOperation(value = "调整盘点准备状态")
    @PostMapping("/switchInventoryPreparation")
    public Response switchInventoryPreparation(@CasUser @ApiIgnore EamUser user, @RequestBody WarehouseInventoryBean bean) {
        return ADOConnection.runTask(user.getCurrDs(), service, "switchInventoryPreparation", Response.class, user, bean);
    }

    @ApiOperation(value = "创建盘点单")
    @PostMapping("/createInventory")
    public Response createInventory(@CasUser @ApiIgnore EamUser user, @RequestBody ToolInventoryBean bean) {
        return ADOConnection.runTask(user.getCurrDs(), service, "createInventory", Response.class, user, bean);
    }

    @ApiOperation(value = "库房历史盘点记录")
    @GetMapping("/getHistoricalRecords")
    public Response<PageInfo> getHistoricalRecords(@CasUser @ApiIgnore EamUser user, ToolInventoryQuery query) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getHistoricalRecords", Response.class, query);
    }

    @ApiOperation(value = "配置盘点周期")
    @PostMapping("/configureInventoryCycle")
    public Response configureInventoryCycle(@CasUser @ApiIgnore EamUser user, @RequestBody WarehouseInventoryBean bean) {
        return ADOConnection.runTask(user.getCurrDs(), service, "configureInventoryCycle", Response.class, bean);
    }

    @ApiOperation(value = "获取盘点单列表")
    @GetMapping("/getInventoryList")
    public Response<PageInfo> getInventoryList(@CasUser @ApiIgnore EamUser user, ToolInventoryQuery query) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getInventoryList", Response.class, query);
    }

    @ApiOperation(value = "取消盘点")
    @GetMapping("/cancelInventory")
    public Response cancelInventory(@CasUser @ApiIgnore EamUser user, ToolInventoryQuery query) {
        return ADOConnection.runTask(user.getCurrDs(), service, "cancelInventory", Response.class, query);
    }

    @ApiOperation(value = "向盘点单零时清单中插入数据")
    @PostMapping("/insertInventoryCheckListTmp")
    public Response insertInventoryCheckListTmp(@CasUser @ApiIgnore EamUser user, @RequestBody List<String> RFIDs) {
        return ADOConnection.runTask(user.getCurrDs(), service, "insertInventoryCheckListTmp", Response.class, user, RFIDs);
    }

    @ApiOperation(value = "获取盘点单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "operation", value = "0:盘点中状态的盘点单详情，1:盘点完成状态的盘点单详情",
                    paramType = "query", dataType = "Integer", defaultValue = "0")
    })
    @GetMapping("/getInventoryDetail")
    public Response getInventoryDetail(@CasUser @ApiIgnore EamUser user, ToolInventoryCheckListQuery query, Integer operation) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getInventoryDetail", Response.class, query, operation);
    }

    @ApiOperation(value = "保存盘点数据")
    @PostMapping("/saveInventory")
    public Response saveInventory(@CasUser @ApiIgnore EamUser user, @RequestBody Map<String,Object> params) {
        Object inventoryId = params.get("inventoryId");
        return ADOConnection.runTask(user.getCurrDs(), service, "saveInventory", Response.class, user, inventoryId);
    }

    @ApiOperation(value = "获取核对页面详情")
    @GetMapping("/getInventoryCheckDetail")
    public Response getInventoryCheckDetail(@CasUser @ApiIgnore EamUser user, ToolInventoryCheckListQuery query) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getInventoryCheckDetail", Response.class, query);
    }

    @ApiOperation(value = "确定盘点")
    @PostMapping("/confirmInventory")
    public Response confirmInventory(@CasUser @ApiIgnore EamUser user, @RequestBody List<ToolInventoryReportBean> beanList) {
        return ADOConnection.runTask(user.getCurrDs(), service, "confirmInventory", Response.class, user, beanList);
    }

    @ApiOperation(value = "获取盘点报告")
    @GetMapping("/getInventoryReport")
    public Response getInventoryReport(@CasUser @ApiIgnore EamUser user, String inventoryId) {
        return ADOConnection.runTask(user.getCurrDs(), service, "getInventoryReport", Response.class, inventoryId);
    }

    @ApiOperation(value = "导出盘点报告")
    @GetMapping("/exportInventoryReport")
    public void exportInventoryReport(@CasUser @ApiIgnore EamUser user, HttpServletResponse response, String inventoryId) {
        try (OutputStream os = response.getOutputStream()) {
            if (null == user) {
                return ;
            }
            Response getInventoryReportResponse = ADOConnection.runTask(user.getCurrDs(), service, "getInventoryReport", Response.class, inventoryId);
            if (getInventoryReportResponse.getCode() != HttpStatus.SC_OK) {
                return ;
            }

            ToolInventoryBean toolInventoryBean = (ToolInventoryBean) getInventoryReportResponse.getData();
            InventoryReportDto inventoryReportDto = InventoryReportConvertor.INSTANCE.toInventoryReportDto(toolInventoryBean);

            // 创建Document
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document, os);
            // 打开document
            document.open();
            PdfPTable innerTable = PdfUtil.createTable(16);
            // 表头字体格式
            Font titleFont = PdfUtil.getFont(12, Font.NORMAL);
            // 表头字体格式
            Font headFont = PdfUtil.getFont(6, Font.NORMAL);
            // 内容字体格式
            Font contentFont = PdfUtil.getFont(6, Font.NORMAL);

            PdfPTable headTable = PdfUtil.createTable(1);
            PdfPCell pdfPCell = PdfUtil.createCell("盘点报告单", titleFont, Element.ALIGN_CENTER, 1, 1);
            pdfPCell.setBorderWidth(0f);
            headTable.addCell(pdfPCell);
            document.add(headTable);

            innerTable.addCell(PdfUtil.createCell("库房名称", headFont, Element.ALIGN_CENTER, 3, 0));
            innerTable.addCell(PdfUtil.createCell(inventoryReportDto.getWarehouseName(), headFont, Element.ALIGN_CENTER, 5, 0));
            innerTable.addCell(PdfUtil.createCell("盘点单号", headFont, Element.ALIGN_CENTER, 3, 0));
            innerTable.addCell(PdfUtil.createCell(inventoryReportDto.getCode(), headFont, Element.ALIGN_CENTER, 5, 0));
            innerTable.addCell(PdfUtil.createCell("开始盘点时间", headFont, Element.ALIGN_CENTER, 3, 0));
            innerTable.addCell(PdfUtil.createCell(inventoryReportDto.getStartTime(), headFont, Element.ALIGN_CENTER, 5, 0));
            innerTable.addCell(PdfUtil.createCell("结束盘点时间", headFont, Element.ALIGN_CENTER, 3, 0));
            innerTable.addCell(PdfUtil.createCell(inventoryReportDto.getEndTime(), headFont, Element.ALIGN_CENTER, 5, 0));
            innerTable.addCell(PdfUtil.createCell("库房工具数量", headFont, Element.ALIGN_CENTER, 3, 0));
            innerTable.addCell(PdfUtil.createCell(String.valueOf(inventoryReportDto.getTotalTools()), headFont, Element.ALIGN_CENTER, 5, 0));
            innerTable.addCell(PdfUtil.createCell("本次盘点数量", headFont, Element.ALIGN_CENTER, 3, 0));
            innerTable.addCell(PdfUtil.createCell(String.valueOf(inventoryReportDto.getTotalInventory()), headFont, Element.ALIGN_CENTER, 5, 0));

            innerTable.addCell(PdfUtil.createCell("序号", headFont, Element.ALIGN_CENTER, 1, 0));
            innerTable.addCell(PdfUtil.createCell("编号", headFont, Element.ALIGN_CENTER, 2, 0));
            innerTable.addCell(PdfUtil.createCell("名称", headFont, Element.ALIGN_CENTER, 2, 0));
            innerTable.addCell(PdfUtil.createCell("型号", headFont, Element.ALIGN_CENTER, 1, 0));
            innerTable.addCell(PdfUtil.createCell("所属单位", headFont, Element.ALIGN_CENTER, 1, 0));
            innerTable.addCell(PdfUtil.createCell("所属库房", headFont, Element.ALIGN_CENTER, 1, 0));
            innerTable.addCell(PdfUtil.createCell("所属货架", headFont, Element.ALIGN_CENTER, 1, 0));
            innerTable.addCell(PdfUtil.createCell("盘点前库存状态", headFont, Element.ALIGN_CENTER, 1, 0));
            innerTable.addCell(PdfUtil.createCell("盘点情况", headFont, Element.ALIGN_CENTER, 1, 0));
            innerTable.addCell(PdfUtil.createCell("盘点后库存状态", headFont, Element.ALIGN_CENTER, 1, 0));
            innerTable.addCell(PdfUtil.createCell("盘点人", headFont, Element.ALIGN_CENTER, 1, 0));
            innerTable.addCell(PdfUtil.createCell("盘点时间", headFont, Element.ALIGN_CENTER, 2, 0));
            innerTable.addCell(PdfUtil.createCell("备注", headFont, Element.ALIGN_CENTER, 1, 0));

            List<InventoryReportDetailDto> inventoryReportDetailDtoList = InventoryReportDetailConvertor.INSTANCE.toInventoryReportDetailDtoList(toolInventoryBean.getInventoryReportBeanList());

            int i = 1;
            for (InventoryReportDetailDto bean : inventoryReportDetailDtoList) {
                innerTable.addCell(PdfUtil.createCell(String.valueOf(i++), headFont, Element.ALIGN_CENTER, 1, 0));
                innerTable.addCell(PdfUtil.createCell(bean.getToolNumber(), headFont, Element.ALIGN_CENTER, 2, 0));
                innerTable.addCell(PdfUtil.createCell(bean.getName(), headFont, Element.ALIGN_CENTER, 2, 0));
                innerTable.addCell(PdfUtil.createCell(bean.getModel(), headFont, Element.ALIGN_CENTER, 1, 0));
                innerTable.addCell(PdfUtil.createCell(bean.getBelongsStationName(), headFont, Element.ALIGN_CENTER, 1, 0));
                innerTable.addCell(PdfUtil.createCell(bean.getWarehouseName(), headFont, Element.ALIGN_CENTER, 1, 0));
                innerTable.addCell(PdfUtil.createCell(bean.getToolTyle(), headFont, Element.ALIGN_CENTER, 1, 0));
                innerTable.addCell(PdfUtil.createCell(bean.getStartStatus(), headFont, Element.ALIGN_CENTER, 1, 0));
                innerTable.addCell(PdfUtil.createCell(bean.getInventorySituation(), headFont, Element.ALIGN_CENTER, 1, 0));
                innerTable.addCell(PdfUtil.createCell(bean.getEndStatus(), headFont, Element.ALIGN_CENTER, 1, 0));
                innerTable.addCell(PdfUtil.createCell(bean.getInventoryPerson(), headFont, Element.ALIGN_CENTER, 1, 0));
                innerTable.addCell(PdfUtil.createCell(bean.getInventoryTime(), headFont, Element.ALIGN_CENTER, 2, 0));
                innerTable.addCell(PdfUtil.createCell(bean.getRemark(), headFont, Element.ALIGN_CENTER, 1, 0));
            }

            document.add(innerTable);
            response.setContentType("application/pdf");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("盘点报告.pdf", "UTF-8"));
            document.close();
            writer.close();
            os.flush();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }


}
